package com.xs.ahoi.hcxs.ui.dialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.dao.BookDao;
import com.xs.ahoi.hcxs.dao.CatalogDao;
import com.xs.ahoi.hcxs.model.Book;
import com.xs.ahoi.hcxs.service.BookFactory;
import com.xs.ahoi.hcxs.utils.FileUtil;
import com.xs.ahoi.hcxs.utils.ObjectUtils;
import com.xs.ahoi.hcxs.utils.StringUtils;

/**
 * 进度弹窗页面
 */
public class DialogPercent extends BaseDialog {

    @BindView(R.id.layoutTips)
    public LinearLayout layoutTips;
    @BindView(R.id.sbPercent)
    public SeekBar sbPercent;
    @BindView(R.id.tvPrev)
    public TextView tvPrev;
    @BindView(R.id.tvNext)
    public TextView tvNext;
    @BindView(R.id.tvCatalog)
    public TextView tvCatalog;
    @BindView(R.id.tvPercent)
    public TextView tvPercent;

    // 书ID
    private int bookId;
    private Book book;
    // 书籍查询工具类
    private BookDao bookDao;
    private CatalogDao catalogDao;
    private DialogPercentListener dialogPercentListener;

    public DialogPercent(@NonNull Context context, int bookId) {
        super(context, R.style.setting_dialog);
        this.bookId = bookId;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_percent;
    }

    @Override
    protected int showPosition() {
        return Gravity.BOTTOM;
    }

    @Override
    protected void initListener() {
        sbPercent.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            int position = 0;

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // 改变时修改进度提示信息
                position = ObjectUtils.toInteger(book.getTotalLenth() * progress / 10000);
                updatePercentInfo(position);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 结束时跳转到对应位置
                position = FileUtil.getValidPosition(position);
                dialogPercentListener.jump(position);
            }
        });
    }

    // 初始化
    @Override
    protected void initData() {
        catalogDao = CatalogDao.getInstance();
        bookDao = BookDao.getInstance();

        updateBook();

        updateView(book.getLastReadPosition());
    }

    @Override
    protected void onStart() {
        super.onStart();
        updateBook();
        updateView(book.getLastReadPosition());
    }

    // 更新书对象
    private void updateBook() {
        book = bookDao.getById(bookId);
    }

    @OnClick({R.id.tvPrev, R.id.tvNext})
    public void onClick(View v) {
        switch (v.getId()) {
            // 上一章
            case R.id.tvPrev:
                Integer prevPosition = BookFactory.getInstance().doPagePrevCatalog();
                if (prevPosition != null) {
                    updateView(prevPosition);
                }
                break;
            // 下一章
            case R.id.tvNext:
                Integer nextPosition = BookFactory.getInstance().doPageNextCatalog();
                if (nextPosition != null) {
                    updateView(nextPosition);
                }
                break;
            default:
                break;
        }
    }

    private void updateView(int position) {
        // 设置进度的默认值
        int sbProgress = ObjectUtils.toInteger(position * 10000f / book.getTotalLenth());
        sbPercent.setProgress(sbProgress);

        updatePercentInfo(position);
    }

    // 进度条位置显示当前进度和章节名
    private void updatePercentInfo(int position) {
        String catalogName = catalogDao.getNameByPosition(bookId, position);
        catalogName = StringUtils.isEmpty(catalogName) ? book.getName() : catalogName;

        tvCatalog.setText(catalogName);
        tvPercent.setText(ObjectUtils.toFix(position * 100f / book.getTotalLenth(), 2) + " %");
    }

    public void setDialogPercentListener(DialogPercentListener dialogPercentListener) {
        this.dialogPercentListener = dialogPercentListener;
    }

    public interface DialogPercentListener {
        void jump(int position);
    }
}
